﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.model;
using l1cs.server.datatables;

namespace l1cs.server.serverpackets
{
    public class S_ShopBuyList : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_ShopBuyList ).FullName );

        public S_ShopBuyList( int objid, L1PcInstance pc )
        {
            L1Object l1object = L1World.getInstance().findObject( objid );
            if ( !( l1object is L1NpcInstance ) )
            {
                return;
            }
            L1NpcInstance npc = (L1NpcInstance)l1object;
            int npcId = npc.getNpcTemplate().get_npcId();
            L1Shop shop = ShopTable.getInstance().get( npcId );
            if ( shop == null )
            {
                pc.sendPackets( new S_NoSell( npc ) );
                return;
            }

            List<L1AssessedItem> assessedItems = shop.assessItems( pc.getInventory() );
            if ( assessedItems.Count == 0 )
            {
                pc.sendPackets( new S_NoSell( npc ) );
                return;
            }

            writeC( Opcodes.S_OPCODE_SHOWSHOPSELLLIST );
            writeD( objid );
            writeH( assessedItems.Count );

            foreach ( L1AssessedItem item in assessedItems )
            {
                writeD( item.getTargetId() );
                writeD( item.getAssessedPrice() );
            }
        }

        override
        public byte[] getContent()
        {
            return getBytes();
        }

        override
        public String getType()
        {
            return "[S] S_ShopBuyList";
        }
    }
}
